<%@ Page Language="VB" ValidateRequest="false" Strict="false" MasterPageFile="~/Default.master"
    Title="Edit Forum Message" %>

<script runat="server">
#Region "Diclaimer"
'Copyright (c) 2006, Mark Bracewell (bracewell at wgg dot com)
'All rights reserved.
'Redistribution and use in source and binary forms, with or without
'modification, are permitted provided that the following conditions are met: 
'*

'* Redistributions of source code must retain the above copyright
'notice, this list of conditions and the following disclaimer.
'* Redistributions in binary form must reproduce the above copyright
'notice, this list of conditions and the following disclaimer in the
'documentation and/or other materials provided with the distribution.
'* The name Mark Bracewell may not be used to endorse or promote products
'derived from this software without specific prior written permission. 
'* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
'WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
'DISCLAIMED. IN NO EVENT SHALL MARK BRACEWELL BE LIABLE FOR ANY
'DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
'(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
'LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
'ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
'(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
'SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
#End Region
    Protected Sub FormView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs)
        Dim subj As String = e.NewValues.Item("subject").ToString()
        If String.IsNullOrEmpty(subj) Then
            subj = "No Subject"
        Else
            subj = HttpUtility.HtmlEncode(subj)
        End If
        e.NewValues.Item("subject") = subj

        Dim body As String = e.NewValues.Item("body").ToString()
        e.NewValues.Item("body") = body
    End Sub

    Protected Sub FormView1_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdatedEventArgs)
        Response.Redirect("Thread.aspx?th=" & e.NewValues("threadid").ToString())
    End Sub

    Protected Sub FormView1_ItemDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewDeletedEventArgs)
        Dim pmid As Integer = CInt(e.Values("parentmsgid"))
        If pmid = 0 Then
            Response.Redirect("Threads.aspx?to=" & e.Values("topicid").ToString())
        Else
            Response.Redirect("Thread.aspx?th=" & e.Values("threadid").ToString())
        End If
    End Sub
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Request("markreadmessageid") Is Nothing = False Then
            ClubStarterKit.Web.Forum.MarkAnswer(CInt(Request("markreadmessageid")))
            If Request("ReturnURL") Is Nothing Then
                Response.Redirect("~/topics.aspx")
            Else
                Response.Redirect(Request("ReturnURL").ToString())
            End If
        End If
        
        If Request("unmarkreadmessageid") Is Nothing = False Then
            ClubStarterKit.Web.Forum.UnmarkAnswer(CInt(Request("unmarkreadmessageid")))
            If Request("ReturnURL") Is Nothing Then
                Response.Redirect("~/topics.aspx")
            Else
                Response.Redirect(Request("ReturnURL").ToString())
            End If
        End If
        Session("FCKeditor:UserFilesPath") = Request.ApplicationPath.ToString & "Files/"
        
        If IsSelf(CStr(Request("ms"))) = False Or User.IsInRole("Moderator") = False Then
            Response.Redirect("~/forum/topics.aspx")
        End If
        
    End Sub
    
    Private Function IsSelf(ByVal messageid As Integer) As Boolean
        Dim CurrentGUID As Guid = Membership.GetUser().ProviderUserKey()
        Dim OrigionalMessageGUID As Guid = ClubStarterKit.Web.Forum.GetMemGuidByMsg(messageid)
        
        If CurrentGUID = OrigionalMessageGUID Then
            Return True
        Else
            Return False
        End If
    End Function
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div id="body">
        <div class="fullwidth">
            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Load" TypeName="ClubStarterKit.Web.Forum" OldValuesParameterFormatString="original_{0}" DataObjectTypeName="ClubStarterKit.Data.ForumMessage" DeleteMethod="Delete" UpdateMethod="SaveEdit">
                <SelectParameters>
                    <asp:QueryStringParameter Name="msgid" Type="Int32" QueryStringField="ms" />
                </SelectParameters>
            </asp:ObjectDataSource>
            
            <asp:FormView DataKeyNames="msgid" ID="FormView1" runat="server" DataSourceID="ObjectDataSource1" Width="100%" OnItemUpdating="FormView1_ItemUpdating" OnItemUpdated="FormView1_ItemUpdated"
                OnItemDeleted="FormView1_ItemDeleted" >
                <EditItemTemplate>
                    <h3>
                        Editing...</h3>
                    <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("msgid") %>' />
                    <asp:HiddenField ID="HiddenField5" runat="server" Value='<%# Bind("threadid") %>' />
                    <p>
                        <asp:Label ID="displaynameLabel" runat="server" Font-Bold="true" Text='<%# Eval("displayname") %>'>
                        </asp:Label><br />
                        <asp:Label ID="postdateLabel" runat="server" Text='<%# Eval("postdate") %>'></asp:Label></p>
                    <p>
                        Subject:
                        <asp:TextBox ID="subjectTextBox" runat="server" Text='<%# Bind("subject") %>' Columns="40">
                        </asp:TextBox></p>
                                       <p><FCKeditorV2:FCKeditor ID="FCKeditor1" runat="server" ToolbarSet = "Default" SkinPath = "skins/office2003/" Value='<%# Bind("body") %>' BasePath="~/FCKeditor/" UseBROnCarriageReturn="true"></FCKeditorV2:FCKeditor></p>

                    <asp:Image ID="acceptedit" runat="server" SkinID="savepost" /> <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                        Text="Save">
                    </asp:LinkButton>
                    <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                        Text="Cancel">
                    </asp:LinkButton>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:HiddenField ID="HiddenField5" runat="server" Value='<%# Bind("threadid") %>' />
                    <asp:HiddenField ID="HiddenField3" runat="server" Value='<%# Bind("topicid") %>' />
                    <asp:HiddenField ID="HiddenField2" runat="server" Value='<%# Bind("parentmsgid") %>' />
                    <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("msgid") %>' />
                    <p>
                        <asp:Label ID="displaynameLabel" runat="server" Text='<%# Eval("displayname") %>'
                            Font-Bold="true">
                        </asp:Label><br />
                        <asp:Label ID="postdateLabel" runat="server" Text='<%# Eval("postdate") %>'></asp:Label></p>
                    <p>
                        Subject:
                        <asp:Label ID="subjectLabel" runat="server" Text='<%# Eval("subject") %>' Font-Bold="true"></asp:Label></p>
                    <div class="msgbody">
                        <asp:Label ID="bodyLabel" runat="server" Text='<%# Eval("body") %>'></asp:Label></div>
                    <br clear="all" />
                    <asp:Image runat="server" ID="edt" SkinID="editthread" /> <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
                        Text="Edit Message">
                    </asp:LinkButton>
                    |
                    <asp:Image ID="del" runat="server" SkinID="deletethread" /> <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                        Text="Delete Message">
                    </asp:LinkButton>
                    |
                    <asp:HyperLink ID="HyperLink1" NavigateUrl='<%# "Thread.aspx?th=" & Eval("threadid").ToString() %>'
                        runat="server">Cancel</asp:HyperLink>
                </ItemTemplate>
            </asp:FormView>
        </div>
    </div>
</asp:Content>
